<script lang="ts" setup>
import { useRoute, useRouter } from 'vue-router'
import ImageItem from '@/components/ImageItem.vue'
import { ShoppingCart } from '@element-plus/icons-vue'
import { useCartStore } from '@/stores/cartStore'
import { useUserStore } from '@/stores/userStore'
import GoodsDrawer from '@/components/GoodsDrawer.vue'
import { updateGoodsApi } from '@/api/goods'
import { ref } from 'vue'

const route = useRoute()
const router = useRouter()
const goodDetailData = JSON.parse(route.query.goods as string)
const imgList = new Array(1).fill(goodDetailData.proImg)
const cartStore = useCartStore()
const userStore = useUserStore()
const goPay = () => {
  router.push('/pay')
}
/**
 * 添加购物车
 */
const addCart = async () => {
  goodDetailData['uid'] = userStore.user.uid
  await cartStore.addCart(goodDetailData)
  ElMessage.success('添加购物车成功')
}
const goodsDrawer = ref()
const openDrawer = () => {
  goodsDrawer.value.drawer = true
}
// 提交修改
const handleSubmit = async (goods) => {
  await updateGoodsApi(goods)
  ElMessage.success('修改成功')
  goodsDrawer.value.drawer = false
}
</script>
<template>
  <div class="xtx-goods-page">
    <div class="container">
      <!-- 商品信息 -->
      <div class="info-container">
        <div>
          <div class="goods-info">
            <div class="media">
              <!-- 图片预览区 -->
              <imageItem :imageList="imgList"></imageItem>
              <!-- 统计数量 -->
              <ul class="goods-sales">
                <li>
                  <p>销量人气</p>
                  <p> 99+ </p>
                  <p>销量人气</p>
                </li>
                <li>
                  <p>收藏人气</p>
                  <p>99+</p>
                  <p>收藏商品</p>
                </li>
              </ul>
            </div>
            <div class="spec">
              <!-- 商品信息区 -->
              <p class="g-name"> {{ goodDetailData.name || '商品名' }}
                <el-button v-if="userStore.user.role===0" :color="'#39c5bb'" icon="Edit" plain round size="small"
                           style="margin-left: 50px" @click="openDrawer">修改商品
                </el-button>
              </p>
              <p class="g-desc">{{ goodDetailData.proMess }} </p>
              <p class="g-price">
                <span> {{ goodDetailData.proPrice?.toFixed(2) }}</span>
              </p>
              <div class="g-service">
                <dl>
                  <dt>促销</dt>
                  <dd>4月好物放送，App领券购买直降120元</dd>
                </dl>
                <dl>
                  <dt>服务</dt>
                  <dd>
                    <span>无忧退货</span>
                    <span>快速退款</span>
                    <span>免费包邮</span>
                    <a href="javascript:">了解详情</a>
                  </dd>
                </dl>
              </div>

              <!-- 按钮组件 -->
              <div>
                <el-button class="btn" color="#39c5bb" plain size="large" @click="goPay">
                  立即购买
                </el-button>
                <el-button :icon="ShoppingCart" class="btn" color="#0FBCE5" plain size="large" style="font-size: 16px"
                           @click="addCart">
                  加入购物车
                </el-button>
              </div>
            </div>
          </div>
        </div>
      </div>

    </div>
  </div>
  <GoodsDrawer ref="goodsDrawer" :goods-data="goodDetailData" :icon="'Edit'" :title="'修改商品'"
               @onSubmit="handleSubmit"></GoodsDrawer>
</template>


<style lang='scss' scoped>
.xtx-goods-page {
  .goods-info {
    min-height: 600px;
    background: #fff;
    display: flex;

    .media {
      width: 580px;
      height: 600px;
      padding: 30px 50px;
    }

    .spec {
      flex: 1;
      padding: 30px 30px 30px 0;
    }
  }

  .goods-footer {
    display: flex;
    margin-top: 20px;
    align-items: center;

    .goods-article {
      width: 100%;
    }

    .goods-aside {
      width: 280px;
      min-height: 1000px;
    }
  }

  .goods-tabs {
    min-height: 600px;
    background: #fff;
  }

  .goods-warn {
    min-height: 600px;
    background: #fff;
    margin-top: 20px;
  }

  .number-box {
    display: flex;
    align-items: center;

    .label {
      width: 60px;
      color: #999;
      padding-left: 10px;
    }
  }

  .g-name {
    font-size: 22px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 10px 0 0;
  }

  .g-desc {
    color: #999;
    margin-top: 10px;
  }

  .g-price {
    margin-top: 10px;

    span {
      &::before {
        content: "¥";
        font-size: 14px;
      }

      &:first-child {
        color: #cf4444;
        margin-right: 10px;
        font-size: 22px;
      }

      &:last-child {
        color: $lt-main-text-color;
        font-size: 20px;
      }
    }
  }

  .g-service {
    background: #f5f5f5;
    width: 500px;
    padding: 20px 10px 0 10px;
    margin-top: 10px;

    dl {
      padding-bottom: 20px;
      display: flex;
      align-items: center;

      dt {
        width: 50px;
        color: #999;
      }

      dd {
        color: #666;

        &:last-child {
          span {
            margin-right: 10px;

            &::before {
              content: "•";
              color: $lt-main-text-color;
              margin-right: 2px;
            }
          }

          a {
            color: $lt-main-text-color;
          }
        }
      }
    }
  }

  .goods-sales {
    display: flex;
    width: 400px;
    align-items: center;
    text-align: center;
    height: 140px;

    li {
      flex: 1;
      position: relative;

      ~ li::after {
        position: absolute;
        top: 10px;
        left: 0;
        height: 60px;
        border-left: 1px solid #e4e4e4;
        content: "";
      }

      p {
        &:first-child {
          color: #999;
        }

        &:nth-child(2) {
          color: #cf4444;
          margin-top: 10px;
        }

        &:last-child {
          color: #666;
          margin-top: 10px;

          i {
            color: $lt-main-color;
            font-size: 14px;
            margin-right: 2px;
          }

          &:hover {
            color: $lt-main-color;
            cursor: pointer;
          }
        }
      }
    }
  }
}

.goods-tabs {
  min-height: 600px;
  background: #fff;

  nav {
    height: 70px;
    line-height: 70px;
    display: flex;
    border-bottom: 1px solid #f5f5f5;

    a {
      padding: 0 40px;
      font-size: 18px;
      position: relative;

      > span {
        color: #cf4444;
        font-size: 16px;
        margin-left: 10px;
      }
    }
  }
}

.goods-detail {
  padding: 40px;

  .attrs {
    display: flex;
    flex-wrap: wrap;
    margin-bottom: 30px;

    li {
      display: flex;
      margin-bottom: 10px;
      width: 50%;

      .dt {
        width: 100px;
        color: #999;
      }

      .dd {
        flex: 1;
        color: #666;
      }
    }
  }

  > img {
    width: 100%;
  }
}

.btn {
  margin-top: 20px;

}

.bread-container {
  padding: 25px 0;
}
</style>
